#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/soundcard.h>
#include <sys/poll.h>
#include <pthread.h>
#include <math.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <linux/fb.h>

#include <jpeglib.h>
#include "bmp_encoder.h"

#include <time.h>

#define IOCTL_LCD_ENABLE_INT	_IO('v', 28)
#define IOCTL_LCD_DISABLE_INT	_IO('v', 29)
#define N 10

typedef struct
{
	int	lcm_fd;
	unsigned char *pLCMBuffer;
	struct fb_var_screeninfo fb_vinfo;
	struct fb_fix_screeninfo fb_finfo;
	unsigned char u8bytes_per_pixel;
} S_DEMO_LCM;

S_DEMO_LCM g_sDemo_Lcm;
/*
int InitLCM()
{
	g_sDemo_Lcm.lcm_fd = open( "/dev/fb0", O_RDWR );
	if ( g_sDemo_Lcm.lcm_fd <= 0 )
	{
		printf( "### Error: cannot open LCM device, returns %d!\n", g_sDemo_Lcm.lcm_fd );
		return 0;
	}

	if ( ioctl(g_sDemo_Lcm.lcm_fd, FBIOGET_VSCREENINFO, &(g_sDemo_Lcm.fb_vinfo)) )
	{
		printf( "ioctl FBIOGET_VSCREENINFO failed!\n" );
		close( g_sDemo_Lcm.lcm_fd );
		return 0;
	}

	g_sDemo_Lcm.u8bytes_per_pixel = g_sDemo_Lcm.fb_vinfo.bits_per_pixel / 8;

	if ( ioctl(g_sDemo_Lcm.lcm_fd, FBIOGET_FSCREENINFO, &(g_sDemo_Lcm.fb_finfo)) )
	{
		printf( "ioctl FBIOGET_FSCREENINFO failed!\n" );
		close( g_sDemo_Lcm.lcm_fd );
        return 0;
    }
	printf( "### LCM Buffer : width = %d, height = %d, line_length = %d.\n\n", g_sDemo_Lcm.fb_vinfo.xres, g_sDemo_Lcm.fb_vinfo.yres, g_sDemo_Lcm.fb_vinfo.bits_per_pixel);
	return 1;
}

int _DemoLCM_SetLCMBuffer()
{
	// Map the device to memory
	g_sDemo_Lcm.pLCMBuffer = mmap( NULL, (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.fb_vinfo.yres * g_sDemo_Lcm.u8bytes_per_pixel), PROT_READ|PROT_WRITE, MAP_SHARED, g_sDemo_Lcm.lcm_fd, 0 );
	if ( (int)g_sDemo_Lcm.pLCMBuffer == -1 )
	{
		printf( "### Error: failed to map LCM device to memory!\n" );
		return 0;
	}
	else
		printf( "### LCM Buffer at:%p, width = %d, height = %d, line_length = %d.\n\n", g_sDemo_Lcm.pLCMBuffer, g_sDemo_Lcm.fb_vinfo.xres, g_sDemo_Lcm.fb_vinfo.yres, g_sDemo_Lcm.fb_finfo.line_length );

	return 1;
}
*/

extern char userbkgnd_bmp[1366*768*4];
extern char userbkgnd_bmp_data[1366*768*4];
int InitLCM(int dis_area_x, int dis_area_y )
{
	g_sDemo_Lcm.pLCMBuffer = (unsigned char *)userbkgnd_bmp_data;
	g_sDemo_Lcm.u8bytes_per_pixel = 16 / 8;
	g_sDemo_Lcm.fb_vinfo.xres = dis_area_x;
	g_sDemo_Lcm.fb_vinfo.yres = dis_area_y;
	return 1;
}


METHODDEF(void)	// 标记，这是一个方法的定义;

my_error_exit( j_common_ptr cinfo )
{
	printf( "### Oops! error occured, here is the message from library. ###\n" );

	(*cinfo->err->output_message) (cinfo);
	jpeg_destroy(cinfo);

	printf( "### Here, you can exit program or not. ###\n" );

	exit(0);
}

//图形块拷贝函数
void copy_picture_block(int width,int height, int x0, int y0, int x1,int y1, int dis_area_x, int dis_area_y, unsigned char *p_Buffer,unsigned char *lcd_Buffer)
{
	unsigned char *src_buffer;
	unsigned char *dest_buffer;
	int z;

	src_buffer = p_Buffer + (y0 * dis_area_x + x0) * g_sDemo_Lcm.u8bytes_per_pixel;
	dest_buffer = lcd_Buffer + (y1 * g_sDemo_Lcm.fb_vinfo.xres + x1) * g_sDemo_Lcm.u8bytes_per_pixel;

	for ( z = 0; z < height; z++ )
	{
		memcpy( dest_buffer, src_buffer, width * g_sDemo_Lcm.u8bytes_per_pixel );
		src_buffer += dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel;
		dest_buffer += g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel;
		
	}
}

void decompress( char * jpeg_filename, int enableScaleDown, int dis_area_x, int dis_area_y, int rotate , int effects)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;

	cinfo.err = jpeg_std_error( &jerr );
	jerr.error_exit = my_error_exit;
	jpeg_create_decompress( &cinfo );

	FILE * infile;
	if ( (infile = fopen( jpeg_filename, "rb" )) == NULL )
	{
		fprintf( stderr, "can't open %s\n", jpeg_filename );
		exit( 1 );
	}

	int scale_num, scale_denom;
	scale_num = scale_denom = 1;
	if ( enableScaleDown )
	{
		fseek( infile, 0L, SEEK_END );
		int sz = ftell( infile );
		fseek( infile, 0L, SEEK_SET );
		unsigned char * temp_jpg = (unsigned char *)malloc( sz );
		static unsigned short u16RealWidth, u16RealHeight, u16YUVFormat;
		unsigned short u16RealWidth2, u16RealHeight2;
		fread( temp_jpg, 1, sz, infile );
		fseek( infile, 0L, SEEK_SET );
		NvtJpeg_GetRealWidthRealHeightYUVFormat( temp_jpg, &u16RealWidth, &u16RealHeight, &u16YUVFormat );
		free( (unsigned char *) temp_jpg );
		u16RealWidth2 = u16RealWidth;
		u16RealHeight2 = u16RealHeight;
		printf( "\tbefore scale width = %d, before scale height = %d\n", u16RealWidth, u16RealHeight );
		float scale_ratio;
		
_FIT_:
		//..........图像缩小............
		if ( (u16RealWidth > dis_area_x) || (u16RealHeight > dis_area_y) )
		{
			if(u16RealWidth > dis_area_x)
			{
				scale_ratio = (float)dis_area_x / (float)u16RealWidth;
				scale_num = (int)dis_area_x;
				scale_denom = (int)u16RealWidth2;
			}
			else
			{
				scale_ratio = (float)dis_area_y / (float)u16RealHeight;	
				scale_num = (int)dis_area_y;
				scale_denom = (int)u16RealHeight2;
				
			}
			u16RealWidth = u16RealWidth * scale_ratio;
			u16RealHeight = u16RealHeight * scale_ratio;
			goto _FIT_;
		}

		//..........图像放大............
		//but hw just support decode scaling down, don't support decode scaling up
		if(0)
		if ( (u16RealWidth < dis_area_x) && (u16RealHeight < dis_area_y) )
		{
			if( (float)dis_area_x / (float)u16RealWidth < (float)dis_area_y / (float)u16RealHeight )
			{
				scale_ratio = (float)dis_area_x / (float)u16RealWidth;
				scale_num = (int)dis_area_x;
				scale_denom = (int)u16RealWidth2;
			}
			else
			{
				scale_ratio = (float)dis_area_y / (float)u16RealHeight;	
				scale_num = (int)dis_area_y;
				scale_denom = (int)u16RealHeight2;
				
			}
			u16RealWidth = u16RealWidth * scale_ratio;
			u16RealHeight = u16RealHeight * scale_ratio;
			goto _FIT_;
		}		
		
		printf( "\tafter scaled width = %d, after scaled height = %d\n", u16RealWidth, u16RealHeight );

	}

	jpeg_stdio_src( &cinfo, infile );

	jpeg_read_header( &cinfo, TRUE );

	cinfo.dct_method = JDCT_IFAST;				// don't care, hw not support
	cinfo.scale_num = scale_num;					// hw just support decode scaling down
	cinfo.scale_denom = scale_denom;			// and hw needs denom great than num
	jpeg_start_decompress( &cinfo );

	printf( "%d, %d, %d\n", cinfo.output_width, cinfo.output_height, cinfo.output_components );

	JSAMPARRAY buffer;
	int row_stride = cinfo.output_width * cinfo.output_components;
	buffer = (*cinfo.mem->alloc_sarray)( (j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1 );

	unsigned char * frame_buffer;
	unsigned char * _frame_buffer;
	frame_buffer = (unsigned char *)malloc( cinfo.output_width * cinfo.output_height * cinfo.output_components );
	_frame_buffer = frame_buffer;

	while ( cinfo.output_scanline < cinfo.output_height )
	{
		jpeg_read_scanlines( &cinfo, buffer, 1 );
		memcpy( _frame_buffer, buffer[0], row_stride );
		_frame_buffer += row_stride;
	}

	jpeg_finish_decompress( &cinfo );

	jpeg_destroy_decompress( &cinfo );

	fclose( infile );

	int i, j, m, display_width, display_height, display_offset, display_offset_y;
	int rotate_flag = 0;
	ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_DISABLE_INT );
	memset( g_sDemo_Lcm.pLCMBuffer, 0, (g_sDemo_Lcm.fb_vinfo.yres * g_sDemo_Lcm.fb_vinfo.yres * g_sDemo_Lcm.u8bytes_per_pixel) );
	
	// hw decode color format default is RGB565 total 16 bits per pixel

	_frame_buffer = frame_buffer;

	unsigned char * g_Buffer;
	unsigned char * _g_Buffer;
	unsigned char * _g_Buffer1;
	g_Buffer = (unsigned char *)malloc(dis_area_x * dis_area_y * g_sDemo_Lcm.u8bytes_per_pixel);
		
	memset( g_Buffer, 0, (dis_area_x * dis_area_y * g_sDemo_Lcm.u8bytes_per_pixel) );

	display_offset = dis_area_x - cinfo.output_width;
	display_width = cinfo.output_width * g_sDemo_Lcm.u8bytes_per_pixel;

	display_offset_y = dis_area_y - cinfo.output_height;
	display_height = cinfo.output_height;

	_g_Buffer = g_Buffer + (display_offset_y/2 * dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel + display_offset/2 * g_sDemo_Lcm.u8bytes_per_pixel);

	for ( i = 0; i < display_height; ++i )
	{
		memcpy( _g_Buffer, _frame_buffer, display_width );
		_g_Buffer += dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel;
		_frame_buffer += display_width;
		
	}

	free( (unsigned char *)frame_buffer );
	
	rotate_flag = rotate;

	if(rotate_flag ==1)//旋转180°处理
	{
		unsigned char * g_Buffer180;
		g_Buffer180 = (unsigned char *)malloc(dis_area_x * dis_area_y * g_sDemo_Lcm.u8bytes_per_pixel);
		memset( g_Buffer180, 0, (dis_area_x * dis_area_y * g_sDemo_Lcm.u8bytes_per_pixel) );
		_g_Buffer = g_Buffer;
		_g_Buffer1 = g_Buffer180+dis_area_x * dis_area_y * g_sDemo_Lcm.u8bytes_per_pixel;
		for ( j = 0; j < dis_area_x*dis_area_y; j++)
		{
			_g_Buffer1 -= g_sDemo_Lcm.u8bytes_per_pixel;
			memcpy( _g_Buffer1, _g_Buffer, g_sDemo_Lcm.u8bytes_per_pixel );
			_g_Buffer  += g_sDemo_Lcm.u8bytes_per_pixel;
		
		}
		_g_Buffer = g_Buffer;
		_g_Buffer1 = g_Buffer180;
		memset( g_Buffer, 0, dis_area_x * dis_area_y * g_sDemo_Lcm.u8bytes_per_pixel );
		memcpy( _g_Buffer, _g_Buffer1, dis_area_x*dis_area_y*g_sDemo_Lcm.u8bytes_per_pixel );

		free( (unsigned char *)g_Buffer180 );

	}

	int w,h,x0,y0,x1,y1;
	unsigned char * _g_sDemo_Lcm ;
	unsigned char * _g_sDemo_Lcm1 ;

	/*switch( effects)//图片特效
	{
		case 0://正常显示
		_g_Buffer = g_Buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		for ( i = 0; i < dis_area_y; ++i )
		{
			memcpy( _g_sDemo_Lcm, _g_Buffer, dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel );
			_g_Buffer += dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel;
			_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel);
		
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
		}
		break;

		case 1://从中心向四周逐次显示
		_g_Buffer = g_Buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;

		for(i=0; i<N/2; i++)
		{
			x0 = dis_area_x/N *(N/2-1-i);
			y0 = dis_area_y/N *(N/2-1-i);
			x1 = dis_area_x/N *(N/2-1-i);
			y1 = dis_area_y/N *(N/2-1-i);
			 w = dis_area_x/N *2*(i+1);
			 h = dis_area_y/N *2*(i+1);
			copy_picture_block(w, h, x0, y0, x1, y1, dis_area_x, dis_area_y, _g_Buffer, _g_sDemo_Lcm);
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(1000*100);	
		}
		break;

		case 2://水平百叶窗显示

		for(i=0; i<dis_area_y/N; i++)
		{
			_g_Buffer = g_Buffer+dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel*i;
			_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer+g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel*i;
			for ( j = 1; j <= N; j++ )
			{
				memcpy( _g_sDemo_Lcm, _g_Buffer, (dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel) );
				_g_Buffer += (dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel)*(dis_area_y/N);
				_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel)*(dis_area_y/N);
			}
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(100);
		}
		break;

		case 3://垂直百叶窗显示

		for(i=0; i<dis_area_x/N; i++)
		{
			_g_Buffer1 = g_Buffer+g_sDemo_Lcm.u8bytes_per_pixel*i;
			_g_sDemo_Lcm1 = g_sDemo_Lcm.pLCMBuffer+g_sDemo_Lcm.u8bytes_per_pixel*i;
			for(m = 0; m < N; m++)
			{
				_g_Buffer = _g_Buffer1+dis_area_x/N * g_sDemo_Lcm.u8bytes_per_pixel*m;
				_g_sDemo_Lcm = _g_sDemo_Lcm1+dis_area_x/N * g_sDemo_Lcm.u8bytes_per_pixel*m;
				for ( j = 0; j < dis_area_y; j++ )
				{
					memcpy( _g_sDemo_Lcm, _g_Buffer,  g_sDemo_Lcm.u8bytes_per_pixel );
					_g_Buffer += (dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel);
					_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel);
					
				}

			}
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(10);
		}
		break;

		case 4://从右下角飞入

		_g_Buffer = g_Buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		
		w=cinfo.output_width/N;
		h=cinfo.output_height/N;
		x0=display_offset/2;
		y0=display_offset_y/2;

		for(j=N-1; j>=0; j--)
		{
			
			x1=display_offset/2+cinfo.output_width*j/10;
			y1=display_offset_y/2+cinfo.output_height*j/10;
			
			copy_picture_block(w, h, x0, y0, x1, y1, dis_area_x, dis_area_y, _g_Buffer, _g_sDemo_Lcm);

			w+=cinfo.output_width/N;
			h+=cinfo.output_height/N;
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(1000*20);
		}
		break;

		case 5://从左上角飞入

		_g_Buffer = g_Buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		
		w=cinfo.output_width/N;
		h=cinfo.output_height/N;
		x1=display_offset/2;
		y1=display_offset_y/2;;

		for(j=N-1; j>=0; j--)
		{
			
			x0=display_offset/2+cinfo.output_width*j/10;
			y0=display_offset_y/2+cinfo.output_height*j/10;
			
			copy_picture_block(w, h, x0, y0, x1, y1, dis_area_x, dis_area_y, _g_Buffer, _g_sDemo_Lcm);

			w+=cinfo.output_width/N;
			h+=cinfo.output_height/N;
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(1000*50);
		}
		break;
		
		default:
		break;


	}
	*/	_g_Buffer = g_Buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		for ( i = 0; i < dis_area_y; ++i )
		{
			memcpy( _g_sDemo_Lcm, _g_Buffer, dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel );
			_g_Buffer += dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel;
			_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel);
		
			//ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
		}
	//ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
	free( (unsigned char *)g_Buffer );
	
	//close( g_sDemo_Lcm.lcm_fd );
}

void display_bmp( char * bmp_filename, int enableScaleDown, int dis_area_x, int dis_area_y, int rotate , int effects)
{
		FILE *p = fopen(bmp_filename,"rb");
		if(p==NULL)
			return;
			
		fseek( p, 0L, SEEK_END );
		int sz = ftell( p );
		fseek( p, 0L, SEEK_SET );
		
		if(sz>(1024*600*4+100))
		{
			printf("BMP Size is bigger than 1024*600*4\n");
			return;			
		}
		
		sz = 1024*600*4+100;
		unsigned char * userbkgnd_bmp = (unsigned char *)malloc( sz );		

		int readlen = 0,i,j,m=0;
		readlen = fread(userbkgnd_bmp,sz,1,p);
		BMPINFO* info = bmp_getinfo(userbkgnd_bmp);
		fclose(p);
		
		int u8bytes_pixel = info->bpp/8;
		unsigned char * frame_buffer;
		
		if(1)
		{	
			if(info->width*u8bytes_pixel%4!=0)
				m=4-info->width*u8bytes_pixel%4;
			
			frame_buffer = (unsigned char *)malloc( sz );
			for(i = 0;i<(int)(info->height);i++)
				memcpy(&frame_buffer[((info->height)-1-i)*u8bytes_pixel*(info->width)],info->pixel_array+i*(info->width*u8bytes_pixel+m),(info->width)*u8bytes_pixel);
		}
		
		memset(userbkgnd_bmp,0,sz);
		unsigned short *ub = (unsigned short *)userbkgnd_bmp;
		if(info->bpp==32)
		{
			for ( i = 0; i < (info->width * info->height); ++i )
			{
				char b = frame_buffer[i*4];
				char g = frame_buffer[i*4 + 1];
				char r = frame_buffer[i*4 + 2];
			
				*ub = ( ( r&0xff )>>3 )<<11 | ( (g&0xff)>>2 )<<5 | (b&0xff )>>3;
				ub++;
			}
		}
		else if(info->bpp==24)
		{
			for ( i = 0; i < (info->width * info->height); ++i )
			{
				char b = frame_buffer[i*3];
				char g = frame_buffer[i*3 + 1];
				char r = frame_buffer[i*3 + 2];
			
				*ub = ( ( r&0xff )>>3 )<<11 | ( (g&0xff)>>2 )<<5 | (b&0xff )>>3;
				ub++;
			}
	
		}
		else if(info->bpp==16)
		{
			
				memcpy(userbkgnd_bmp,frame_buffer,info->height*info->width*u8bytes_pixel);
					
		}	
		int display_offset;
		int display_offset_y;
		int display_width;
		int display_height;
		unsigned char * _g_Buffer;
		unsigned char * _g_sDemo_Lcm;
		unsigned char * _g_Buffer1;
		unsigned char * _g_sDemo_Lcm1;
		
		int w=0,h=0,x0=0,y0=0,x1=0,y1=0;
			
			
		if(1)
		{			
		
			if(info->width>dis_area_x)
				display_width = dis_area_x;
			else
				display_width = info->width;
			
			if(info->height>dis_area_y)
				display_height = dis_area_y;
			else
				display_height = info->height;
		
			display_offset = (dis_area_x - display_width)/2;
			display_offset_y = (dis_area_y - display_height)/2;
			
			memset(frame_buffer,0,sz);
			_g_Buffer = userbkgnd_bmp;
			_g_sDemo_Lcm = frame_buffer+(display_offset+display_offset_y*dis_area_x)*2;
			for ( i = 0; i < display_height; ++i )
			{
				memcpy( _g_sDemo_Lcm, _g_Buffer, display_width*2 );
				_g_Buffer += info->width*2;
				_g_sDemo_Lcm += dis_area_x * 2;
			}
		}
		
		
		
		if(rotate==1)
		{
			unsigned char * g_Buffer180;
			g_Buffer180 = (unsigned char *)malloc(dis_area_x * dis_area_y * 2);
			memset( g_Buffer180, 0, dis_area_x * dis_area_y * 2 );
			_g_Buffer = frame_buffer;
			char* _g_Buffer1 = g_Buffer180+dis_area_x * dis_area_y * 2;
			for ( i = 0; i< dis_area_x * dis_area_y; i++)
			{
				_g_Buffer1 -= 2;
				memcpy( _g_Buffer1, _g_Buffer, 2 );
				_g_Buffer  += 2;

			}
			_g_Buffer = frame_buffer;
			_g_Buffer1 = g_Buffer180;
			memset( _g_Buffer, 0, sz );
			memcpy( _g_Buffer, _g_Buffer1, dis_area_x * dis_area_y * 2 );

			free( (unsigned char *)g_Buffer180 );
		}
		
		
	/*switch( effects)//图片特效
	{
		case 1://从中心向四周逐次显示
		_g_Buffer = frame_buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;

		for(i=0; i<N/2; i++)
		{
			x0 = dis_area_x/N *(N/2-1-i);
			y0 = dis_area_y/N *(N/2-1-i);
			x1 = dis_area_x/N *(N/2-1-i);
			y1 = dis_area_y/N *(N/2-1-i);
			 w = dis_area_x/N *2*(i+1);
			 h = dis_area_y/N *2*(i+1);
			copy_picture_block(w, h, x0, y0, x1, y1, dis_area_x, dis_area_y, _g_Buffer, _g_sDemo_Lcm);
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(1000*100);	
		}
		break;

		case 2://水平百叶窗显示

		for(i=0; i<dis_area_y/N; i++)
		{
			_g_Buffer = frame_buffer+dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel*i;
			_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer+g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel*i;
			for ( j = 1; j <= N; j++ )
			{
				memcpy( _g_sDemo_Lcm, _g_Buffer, (dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel) );
				_g_Buffer += (dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel)*(dis_area_y/N);
				_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel)*(dis_area_y/N);
			}
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(100);
		}
		break;

		case 3://垂直百叶窗显示

		for(i=0; i<dis_area_x/N; i++)
		{
			_g_Buffer1 = frame_buffer+g_sDemo_Lcm.u8bytes_per_pixel*i;
			_g_sDemo_Lcm1 = g_sDemo_Lcm.pLCMBuffer+g_sDemo_Lcm.u8bytes_per_pixel*i;
			for(m = 0; m < N; m++)
			{
				_g_Buffer = _g_Buffer1+dis_area_x/N * g_sDemo_Lcm.u8bytes_per_pixel*m;
				_g_sDemo_Lcm = _g_sDemo_Lcm1+dis_area_x/N * g_sDemo_Lcm.u8bytes_per_pixel*m;
				for ( j = 0; j < dis_area_y; j++ )
				{
					memcpy( _g_sDemo_Lcm, _g_Buffer,  g_sDemo_Lcm.u8bytes_per_pixel );
					_g_Buffer += (dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel);
					_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel);
					
				}

			}
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(10);
		}
		break;

		case 4://从右下角飞入

		_g_Buffer = frame_buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		
		w=dis_area_x/N;
		h=dis_area_y/N;
		x0=0;
		y0=0;

		for(j=N-1; j>=0; j--)
		{
			
			x1=dis_area_x*j/10;
			y1=dis_area_y*j/10;
			
			copy_picture_block(w, h, x0, y0, x1, y1, dis_area_x, dis_area_y, _g_Buffer, _g_sDemo_Lcm);

			w+=dis_area_x/N;
			h+=dis_area_y/N;
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(1000*20);
		}
		break;

		case 5://从左上角飞入

		_g_Buffer = frame_buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		
		w=dis_area_x/N;
		h=dis_area_y/N;
		x1=0;
		y1=0;

		for(j=N-1; j>=0; j--)
		{
			
			x0=dis_area_x*j/10;
			y0=dis_area_y*j/10;
			
			copy_picture_block(w, h, x0, y0, x1, y1, dis_area_x, dis_area_y, _g_Buffer, _g_sDemo_Lcm);

			w+=dis_area_x/N;
			h+=dis_area_y/N;
			ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
			usleep(1000*50);
		}
		break;
		
		default:
		break;
	}	
	*/	
		_g_Buffer = frame_buffer;
		_g_sDemo_Lcm = g_sDemo_Lcm.pLCMBuffer;
		for ( i = 0; i < dis_area_y; ++i )
		{
			memcpy( _g_sDemo_Lcm, _g_Buffer, dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel );
			_g_Buffer += dis_area_x * g_sDemo_Lcm.u8bytes_per_pixel;
			_g_sDemo_Lcm += (g_sDemo_Lcm.fb_vinfo.xres * g_sDemo_Lcm.u8bytes_per_pixel);
			//ioctl( g_sDemo_Lcm.lcm_fd, IOCTL_LCD_ENABLE_INT );
		}
		free(frame_buffer);
		free(userbkgnd_bmp);	
		return;

}

int jpg_bmp_display(char * bmp_filename, int dis_area_x, int dis_area_y, int rotate)
{
	if( (strstr(bmp_filename,"bmp")!=NULL) || (strstr(bmp_filename,"BMP")!=NULL) )
		display_bmp(bmp_filename, 1, dis_area_x, dis_area_y, rotate, 0);	
	else
		decompress(bmp_filename, 1, dis_area_x, dis_area_y, rotate, 0);

}

/*display_bmp( char * bmp_filename, int enableScaleDown, int dis_area_x, int dis_area_y, int rotate , int effects);

int main( int argc, char *argv[] )
{
	srand((unsigned)time(NULL)); //产生随机种子

	if ( !InitLCM() )
		exit( 1 );

	if ( !_DemoLCM_SetLCMBuffer() )
		exit( 1 );

	switch ( argc )
	{

	case 4:
		if( (strstr(argv[1],"bmp")!=NULL) || (strstr(argv[1],"BMP")!=NULL) )
			display_bmp(argv[1], 1, atoi( argv[2] ), atoi( argv[3] ), 0,rand()%6);	
		else
			decompress( argv[1], 1, atoi( argv[2] ), atoi( argv[3] ), 0,rand()%6);
		break;
		
	case 5:
		if( (strstr(argv[1],"bmp")!=NULL) || (strstr(argv[1],"BMP")!=NULL) )
			display_bmp(argv[1], 1, atoi( argv[2] ), atoi( argv[3] ), atoi( argv[4] ), rand()%6);	
		else
			decompress( argv[1], 1, atoi( argv[2] ), atoi( argv[3] ), atoi( argv[4] ), rand()%6);
		break;

	case 6:
		if( (strstr(argv[1],"bmp")!=NULL) || (strstr(argv[1],"BMP")!=NULL) )
			display_bmp(argv[1], 1, atoi( argv[2] ), atoi( argv[3] ), atoi( argv[4] ), atoi( argv[5] ));
		else
			decompress( argv[1], 1, atoi( argv[2] ), atoi( argv[3] ), atoi( argv[4] ), atoi( argv[5] ));
		break;

	default:
		printf( "Usage:\t%s arg1 arg2 arg3 arg4 arg5 \n", argv[0] );
		printf( "Option:\n" );
		printf( "argv1:\tinput JPEG filename, like \"my_jpeg.jpg\" or others\n" );
		printf( "argv2:\tdisplay_x, like \"800\"\n" );
		printf( "argv3:\tdisplay_y, like \"480\"\n" );
		printf( "argv4:\trotate_enable, like \"1\" rotate 180°, like \"0\" rotate_disable\n" );
		printf( "argv5:\teffects \"0\" - \"5\" \n" );
		break;
	}
	close( g_sDemo_Lcm.lcm_fd );
	sleep(5);
	return 0;
}*/

